package logger

import (
	"context"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
)

type LoggerContext struct {
	logger *zap.Logger
	ctx    context.Context
}

func (l *LoggerContext) NewLoggerWithLevel() *LoggerContext {
	// 设置日志级别
	atomicLevel := zap.NewAtomicLevel()
	atomicLevel.SetLevel(zap.DebugLevel)
	//公用编码器
	encoderConfig := zapcore.EncoderConfig{
		TimeKey:        "time",
		LevelKey:       "level",
		NameKey:        "logger",
		CallerKey:      "linenum",
		MessageKey:     "msg",
		StacktraceKey:  "stacktrace",
		LineEnding:     zapcore.DefaultLineEnding,
		EncodeLevel:    zapcore.LowercaseLevelEncoder,  // 小写编码器
		EncodeTime:     zapcore.ISO8601TimeEncoder,     // ISO8601 UTC 时间格式
		EncodeDuration: zapcore.SecondsDurationEncoder, //
		EncodeCaller:   zapcore.FullCallerEncoder,      // 全路径编码器
		EncodeName:     zapcore.FullNameEncoder,
	}

	var multiWriteSyncer []zapcore.WriteSyncer

	multiWriteSyncer = append(multiWriteSyncer, zapcore.AddSync(os.Stdout), zapcore.AddSync(os.Stderr)) //打印到控制台
	//multiWriteSyncer = append(multiWriteSyncer, zapcore.AddSync(newLumberjackSink())) //打印到文件,生产环境开启

	core := zapcore.NewCore(
		zapcore.NewJSONEncoder(encoderConfig),            // 编码器配置
		zapcore.NewMultiWriteSyncer(multiWriteSyncer...), // 打印到控制台和文件
		atomicLevel, // 日志级别
	)

	// 开启开发模式，堆栈跟踪
	caller := zap.AddCaller()
	//我们稍微封装了一下记录日志的方法，但是我们希望输出的文件名和行号是调用封装函数的位置。这时可以使用zap.AddCallerSkip(skip int)向上跳 1 层
	skip := zap.AddCallerSkip(1)
	// 开启文件及行号
	development := zap.Development()
	l.logger = zap.New(core, caller, skip, development)
	return l
}

func (l *LoggerContext) Debug(ctx context.Context, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Info(fmt.Sprint(args...))
}

func (l *LoggerContext) Debugf(ctx context.Context, format string, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Debug(fmt.Sprintf(format, args...))
}

func (l *LoggerContext) Info(ctx context.Context, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Info(fmt.Sprint(args...))
}

func (l *LoggerContext) Infof(ctx context.Context, format string, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Info(fmt.Sprintf(format, args...))
}

func (l *LoggerContext) Warn(ctx context.Context, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Warn(fmt.Sprint(args...))
}

func (l *LoggerContext) Warnf(ctx context.Context, format string, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Warn(fmt.Sprintf(format, args...))
}

func (l *LoggerContext) Error(ctx context.Context, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Error(fmt.Sprint(args...))
}

func (l *LoggerContext) Errorf(ctx context.Context, format string, args ...interface{}) {
	//l.WithContext(ctx.(*gin.Context)).Error(fmt.Sprintf(format, args...))
}
